package com.transportation.app.see

import android.util.Log
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.internal.sse.RealEventSource
import okhttp3.sse.EventSourceListener
import java.util.concurrent.TimeUnit

class SseManager {
    private var eventSource: RealEventSource? = null

    fun startListening(url: String, listener: EventSourceListener) {
        val request: Request = Request.Builder()
            .url(url)
            .build()
        val okHttpClient = OkHttpClient.Builder().also {
            it.connectTimeout(1, TimeUnit.DAYS)
            it.readTimeout(1, TimeUnit.DAYS)
        }.build()
        eventSource = RealEventSource(request, listener)
        eventSource?.connect(okHttpClient)
        Log.d("SseListener", "-startListening--startListening->:")
    }

    fun stopListening() {
        eventSource?.cancel()
    }
}